<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserBind
 *
 * @author Administrator
 */

namespace frontend\behavior;

use yii\base\Behavior;
use Yii;
use frontend\library\oauth2\OAuth2_Token;
use frontend\library\oauth2\providers\OAuth2_Qq;
use frontend\library\oauth2\providers\OAuth2_Weibo;
use frontend\enum\ThirdType;

class UserBindBehavior extends Behavior {

    //put your code here
    public function validate() {
      
        $token = new OAuth2_Token($this->owner->oauth_token);
        if ($this->owner->type == ThirdType::QQ) {
            $provider = new OAuth2_Qq(Yii::$app->params['oauth']);
        } elseif ($this->owner->type ==ThirdType::WeiBo) {
            $provider = new OAuth2_Weibo(Yii::$app->params['oauth']);
        }
        $third_uid = $provider->getUserId($token);
        if ($third_uid != $this->owner->type_uid) {
            throw new UserException(UserException::E_OAUTH_UID_INVAILD);
        }else{
            return true;
        }
    }

}
